package com.yx.changdao.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.NoWorkday;
import com.yx.changdao.common.enums.NoWorkMonthEnum;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.Faster;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.service.NoWorkdayService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: <br/>
 * 非工作日表
 * <p>
 * <br/>
 * @Auther: walter
 * @create 2020/04/30 10:45
 */
@RestController
@RequestMapping("no/workday")
public class NoWorkdayController {

    /**
     * 非工作日表服务
     */
    @Autowired
    private NoWorkdayService noWorkdayService;

    /**
     * 列表
     *
     * @param year 年份：2020
     * @param month 月份：五月     注意格式：五月非5/05
     * @return
     * @throws AlertException
     */
    @AutoLog
    @GetMapping("/list")
    @RequiresPermissions("no:workday:list")
    @ShiroDesc("非工作日表:列表")
    public Result list(@RequestParam("year") String year, @RequestParam("month") String month) throws AlertException {
        if (4 != StringUtils.length(year)) {
            throw new AlertException("年格式不正确");
        }
        String value = NoWorkMonthEnum.getValue(month);
        if(null == value){
            throw new AlertException("月格式不正确");
        }
        String date = year + "-" + value;
        List<NoWorkday> list = noWorkdayService.list(new QueryWrapper<NoWorkday>().likeRight(NoWorkday.NO_WORKDAY, date).orderByAsc(NoWorkday.DAY_TIMESTAMP));
        ArrayList<Map<String, Long>> dayTimestamps = new ArrayList<>();
        list.forEach(n -> {
            Map<String, Long> map = new HashMap<>();
            map.put("dayTimestamp", n.getDayTimestamp());
            dayTimestamps.add(map);
        });
        return Result.ok().data(dayTimestamps);
    }

    /**
     * 添加非工作日表
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PostMapping
  /*  @RequiresPermissions("no:workday:add")
    @ShiroDesc("非工作日表:添加")*/
    public Result add(@RequestParam("dayTimestamp") Long dayTimestamp) throws AlertException {
        String date = timestampToDate(dayTimestamp);
        int count = noWorkdayService.count(new QueryWrapper<NoWorkday>().eq(NoWorkday.NO_WORKDAY, date));
        if (count > 0) {
            throw new AlertException("已存在的信息");
        }
        System.out.println(date);
        NoWorkday workday = new NoWorkday();
        workday.setNoWorkday(date);
        workday.setDayTimestamp(dayTimestamp);
        workday.setCreateBy(SysUtils.currentUserId());
        workday.setCreateTime(Faster.now());
        boolean b = noWorkdayService.save(workday);

        if (!b) {
            throw new AlertException("非工作日表添加失败");
        }

        return Result.ok("非工作日表添加成功");
    }

    private String timestampToDate(Long dayTimestamp){
        DateFormat t = new SimpleDateFormat("yyyy-MM-dd");
        return t.format(new Date(dayTimestamp));
    }
    /**
     * 获得指定id 非工作日表。
     *
     * @param
     * @return Result
     */
    @AutoLog
    @GetMapping("/{id}")
    @RequiresPermissions("no:workday:look")
    @ShiroDesc("非工作日表:查看")
    public Result get(@PathVariable("id") String id) throws AlertException {
        NoWorkday noWorkday = noWorkdayService.getById(id);

        if (noWorkday == null) {
            throw new AlertException("未找到相关非工作日表信息");
        }

        return Result.ok().data(noWorkday);
    }


    /**
     * 更新非工作日表
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PutMapping
   /* @RequiresPermissions("no:workday:save")
    @ShiroDesc("非工作日表:保存")*/
    public Result update(NoWorkday noWorkday) throws AlertException {
        boolean b = noWorkdayService.updateById(noWorkday);
        if (!b) {
            throw new AlertException("非工作日表更新失败");
        }

        return Result.ok("非工作日表更新成功");
    }


    /**
     * 删除非工作日表
     *
     * @param
     * @return Result
     */
    @AutoLog
    @DeleteMapping()
/*    @RequiresPermissions("no:workday:del")
    @ShiroDesc("非工作日表:删除")*/
    public Result delete(@RequestParam("dayTimestamp") Long dayTimestamp) throws AlertException {
        String date = timestampToDate(dayTimestamp);
        boolean b = noWorkdayService.removeById(date);
        if (!b) {
            throw new AlertException("非工作日表删除失败");
        }

        return Result.ok("删除非工作日表成功");
    }

}
